package com.lancoo.answer.util

import android.animation.ValueAnimator
import android.app.ActionBar
import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.LinearInterpolator
import android.view.animation.Transformation
import android.widget.TextView
import com.lancoo.answer.R

object LancooAnimUtils {

    //纵向展开
    fun expand(v: View) {
        val matchParentMeasureSpec =
            View.MeasureSpec.makeMeasureSpec((v.parent as View).width, View.MeasureSpec.EXACTLY)
        val wrapContentMeasureSpec =
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        v.measure(matchParentMeasureSpec, wrapContentMeasureSpec)
        val targetHeight = v.measuredHeight

        // Older versions of android (pre API 21) cancel animations for views with a height of 0.
        v.layoutParams.height = 1
        v.visibility = View.VISIBLE
        val a: Animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                v.layoutParams.height =
                    if (interpolatedTime == 1f) ActionBar.LayoutParams.WRAP_CONTENT else (targetHeight * interpolatedTime).toInt()
                v.requestLayout()
            }

            override fun willChangeBounds(): Boolean {
                return true
            }
        }

        // Expansion speed of 1dp/ms
        a.duration = ((targetHeight / v.context.resources.displayMetrics.density).toLong())
        v.startAnimation(a)
    }

    //纵向收起
    fun collapse(v: View) {
        val initialHeight = v.measuredHeight
        val a: Animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                if (interpolatedTime == 1f) {
                    v.visibility = View.GONE
                } else {
                    v.layoutParams.height =
                        initialHeight - (initialHeight * interpolatedTime).toInt()
                    v.requestLayout()
                }
            }

            override fun willChangeBounds(): Boolean {
                return true
            }
        }

        // Collapse speed of 1dp/ms
        a.duration = ((initialHeight / v.context.resources.displayMetrics.density).toLong())
        v.startAnimation(a)
    }

    //数字子增长动画
    fun setNumberIntAnim(mTv: TextView, startNumber: Int, endNumber: Int, duration: Long) {
        val animator = ValueAnimator.ofInt(startNumber, endNumber)
        animator.duration = duration //时长5s
        animator.interpolator = LinearInterpolator() //线性插值器
        animator.addUpdateListener { animation ->
            mTv.text = animation.animatedValue.toString()
        } //监听
        animator.start()

//        val animator = ValueAnimator.ofInt(startNumber, endNumber)
//        animator.duration = duration
//        animator.addUpdateListener { animation -> mTv.text = animation.animatedValue.toString() }
//        animator.start()

    }

    /**
     * 缩放变大动画
     *
     * @param context
     * @param view 目标view
     */
    fun startScaleBigAnim(context: Context?, view: View?) {
        val animation: Animation = AnimationUtils.loadAnimation(context, R.anim.ev_anim_scale_big)
        view?.startAnimation(animation)
    }

    /**
     * 缩放缩小动画
     *
     * @param context
     * @param view 目标view
     */
    fun startScaleSmallAnim(context: Context?, view: View?) {
        val animation: Animation = AnimationUtils.loadAnimation(context, R.anim.ev_anim_scale_small)
        view?.startAnimation(animation)
    }

    /**
     * 缩放变大动画
     *
     * @param context
     * @param view 目标view
     */
    fun startVerticalScaleBigAnim(context: Context?, view: View?) {
        val animation: Animation =
            AnimationUtils.loadAnimation(context, R.anim.ev_anim_vertical_scale_big)
        view?.startAnimation(animation)
    }

    /**
     * 缩放缩小动画
     *
     * @param context
     * @param view 目标view
     */
    fun startVerticalScaleSmallAnim(context: Context?, view: View?) {
        val animation: Animation =
            AnimationUtils.loadAnimation(context, R.anim.ev_anim_vertical_scale_small)
        view?.startAnimation(animation)
    }

}